package org.gwc.config;

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.logging.slf4j.Slf4jImpl;
import org.apache.ibatis.session.AutoMappingBehavior;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * 方式1：不保留外部配置文件，全部mybatis的属性都在代码中设置
 *
 */

@Configuration
public class MapperJavaConfigNew {

    /**
     * sqlSessionFactory加入ioc容器
     * mybatis -> sqlSessionFactory[ioc] -> getObject() -> sqlSessionFactory
     *
     */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //指定数据库连接池对象
        sqlSessionFactoryBean.setDataSource(dataSource);
        //指定mybatis配置文件功能，使用代码的形式
        /**
         * <settings>
         *         开启驼峰式映射
         *         <setting name="mapUnderscoreToCamelCase" value="true"/>
         *         开启logback日志输出
         *         <setting name="logImpl" value="SLF4J"/>
         *       开启resultMap自动映射
         *         <setting name="autoMappingBehavior" value="FULL"/>
         *     </settings>
         */
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setLogImpl(Slf4jImpl.class);
        configuration.setAutoMappingBehavior(AutoMappingBehavior.FULL);
        // 存储setting的配置文件
        sqlSessionFactoryBean.setConfiguration(configuration);

        /**
         * <typeAliases>
         *         给实体类起别名
         *         <package name="org.gwc.pojo"/>
         *     </typeAliases>
         */
        // 别名设置
        sqlSessionFactoryBean.setTypeAliasesPackage("org.gwc.pojo");
        // 插件设置
        /**
         * <plugins>
         *         <plugin interceptor="com.github.pagehelper.PageInterceptor">
         *
         *             <property name="helperDialect" value="mysql"/>
         *         </plugin>
         *     </plugins>
         */
        PageInterceptor pageInterceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect","mysql");
        pageInterceptor.setProperties(properties);
        sqlSessionFactoryBean.addPlugins(pageInterceptor);


        return sqlSessionFactoryBean;
    }

    // mapper 代理对象加入到ioc, 不懂这里是怎么跟sqlSessionFactoryBean 进行关联的
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        // mapper 代理兑现的factoryBean->指定一个包->mapper接口->sqlSessionFactory->sqlSession->getMapper->mapper代理对象->ioc
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("org.gwc.mapper"); // mapper接口和mapperxml所在的共同包
        return mapperScannerConfigurer;
    }

}
